package com.scm.erp.framework.controller;

import com.scm.erp.framework.pojo.tPermissions;
import com.scm.erp.framework.pojo.tRole;
import com.scm.erp.framework.pojo.tUser;
import com.scm.erp.framework.repository.tPermissionsRepository;
import com.scm.erp.framework.repository.tRoleRepository;
import com.scm.erp.framework.repository.tUserRepository;
import com.scm.erp.framework.tools.CloudDetectionTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 * 控制用户增删改查
 *
 * */

@Controller
public class tUserController {
    @Resource
    private tUserRepository userRepository;
    @Resource
    private tRoleRepository roleRepository;
    @Resource
    private tPermissionsRepository permissionsRepository;
    private CloudDetectionTools cloudDetectionTools = new CloudDetectionTools();

    /**
     * 创建上帝管理员！
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/createRoot")
    public String createRootAction(){

        Set<tUser> userList = userRepository.queryAllByAccount("15880973723");
        System.out.println(userList.size());
        if(userList.size()>0){
            for (tUser u: userList){
                if("".equals(u)==false){
                    System.out.println(u.toString());
                    Set<tRole> roleSet = u.getuRoleSet();
                    for(tRole r:roleSet){
                        if("".equals(r)==false){
                            System.out.println(r.toString());
                            Set<tPermissions> permissionsSet = r.getpPermissionsSet();
                            for(tPermissions p:permissionsSet){
                                if("".equals(p)==false){
                                    System.out.println(p.toString());
                                    return "{code:0,staus:0,message:[username:" +u.getuAccount() +",password:" + u.getuPassword() +"]}";
                                }
                            }
                        }
                    }
                }
            }
        }else {
            tUser user = new tUser();
            tRole role = new tRole();
            tPermissions Permissions = new tPermissions();
            Set<tRole> roleSet = new HashSet<>();
            Set<tUser> userSet = new HashSet<>();
            Set<tPermissions> permissionsSet = new HashSet<>();

            user.setuAccount("15880973723");
            user.setU_Email("1012518027@qq.com");
            SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
            user.setuBirthday(sdf.format(new Date()));
            user.setuCard("-");
            user.setuName("上帝");
            user.setuPassword("admin");
            user.setuDate(new Date());
            user.setuEnabled(true);
            user.setuAccountNonLocked(true);
            user.setuCredentialsNonExpired(true);
            user.setuAccountNonExpired(true);

            role.setrAccount(user.getuAccount());
            role.setrDescribe("上帝管理员模式可管理所有权限");
            role.setrUserRoles("超级管理员");

            Permissions.setpAccount(user.getuAccount());
            Permissions.setpRoleAuthorization("上帝模式");
            Permissions.setpPermissionIdentifier("ROLE_GOD");

            user.setuRoleSet(roleSet);
            role.setuUserSet(userSet);
            role.setpPermissionsSet(permissionsSet);
            Permissions.setrRoleSet(roleSet);

            userSet.add(user);
            roleSet.add(role);
            permissionsSet.add(Permissions);

            userRepository.save(user);
            roleRepository.save(role);
            permissionsRepository.save(Permissions);
            return "{code:0,staus:0,message:insert}";
        }
        return "{code:0,staus:0,message:error}";
    };
}
